using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Runtime;
using Microsoft.SharePoint.WorkflowActions;

namespace DP.Sharepoint.Workflow
{
    public partial class ResetListItemPermissionInheritance : Activity
    {
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                         typeof (WorkflowContext),
                                                                                         typeof (
                                                                                             ResetListItemPermissionInheritance
                                                                                             ));


        public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof (string),
                                                                                      typeof (
                                                                                          ResetListItemPermissionInheritance
                                                                                          ));

        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof (int),
                                                                                        typeof (
                                                                                            ResetListItemPermissionInheritance
                                                                                            ));

        public ResetListItemPermissionInheritance()
        {
            InitializeComponent();
        }

        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext) (base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

        [Description("ListId")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ListId
        {
            get { return ((string) (base.GetValue(ListIdProperty))); }
            set { base.SetValue(ListIdProperty, value); }
        }

        [Description("ListItem")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int ListItem
        {
            get { return ((int) (base.GetValue(ListItemProperty))); }
            set { base.SetValue(ListItemProperty, value); }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                PermissionRequest myResetRequest = new PermissionRequest();

                myResetRequest.RequestType = PermissionActionType.Reset;
                myResetRequest.ItemId = ListItem;
                myResetRequest.ListID = new Guid(ListId);
                myResetRequest.SiteID = __Context.Site.ID;
                myResetRequest.WebID = __Context.Web.ID;


                WorkflowEnvironment.WorkBatch.Add(PermissionsService.Instance, myResetRequest);


                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //     {
                //         using (SPSite site = new SPSite(__Context.Site.ID))
                //         {
                //             using (SPWeb web = site.AllWebs[__Context.Web.ID])
                //             {


                //                 SPList List = web.Lists[new Guid(this.ListId)];

                //                 SPListItem listItem = List.GetItemById(this.ListItem);

                //                 if (listItem.HasUniqueRoleAssignments)
                //                 {
                //                     listItem.ResetRoleInheritance();

                //                     listItem.SystemUpdate();

                //                 }


                //             }
                //         }

                //     });
            }
            catch (Exception e)
            {
                Common.LogExceptionToWorkflowHistory(e, executionContext, WorkflowInstanceId);

                throw;
            }
            return base.Execute(executionContext);
        }
    }
}